package com.dhcc.core.modules.system.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dhcc.core.framework.cache.SysCache;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.util.CommonUtil;

/**
 * 缓存管理控制器
 * 
 * @ClassName: CacheController
 * @Description: TODO
 * @author: cyf
 * @date: 2018年1月2日 上午10:09:21
 */
@Controller
@RequestMapping("${sys.backendPath}/cache")
public class CacheController {

    private String PREFIX = "/backend/system/cache/";

    @RequestMapping
    public String index(Model model) {
        model.addAttribute("caches", SysCache.me().getAllCacheNames());
        return PREFIX + "cache.html";
    }

    @RequestMapping("/clear/{cacheName}")
    @ResponseBody
    public R clear(@PathVariable("cacheName") String cacheName) {
        if (CommonUtil.isEmpty(cacheName))
            throw new BizException("缓存名称不能为空！");
        SysCache.me().removeByCacheName(cacheName);
        return R.ok("清理成功！");
    }

    @RequestMapping("/clear/all")
    @ResponseBody
    public R clear() {
        SysCache.me().removeAll();
        return R.ok("清理成功！");
    }

}